package settings_api

import (
	"gin_study_blog/common"
	"gin_study_blog/common/http"
	"gin_study_blog/config"
	"gin_study_blog/core"
	"github.com/gin-gonic/gin"
)

// SettingsApiModifyView 修改 site_info 信息
func (SettingsApi) SettingsApiModifyView(c *gin.Context) {
	var uri SettingUri
	err := c.ShouldBindUri(&uri)
	if err != nil {
		http.FailWithCode(http.ParamsError, c)
		return
	}

	switch uri.Name {
	case "site":
		var info config.SiteInfo
		// 绑定 json 参数
		modifyData := c.ShouldBindJSON(&info)
		if modifyData != nil {
			http.FailWithCode(http.ParamsError, c)
			return
		}
		common.Config.SiteInfo = info

	case "email":
		var info config.Email
		// 绑定 json 参数
		modifyData := c.ShouldBindJSON(&info)
		if modifyData != nil {
			http.FailWithCode(http.ParamsError, c)
			return
		}
		common.Config.Email = info

	case "qq":
		var info config.QQ
		// 绑定 json 参数
		modifyData := c.ShouldBindJSON(&info)
		if modifyData != nil {
			http.FailWithCode(http.ParamsError, c)
			return
		}
		common.Config.QQ = info
	case "qiniu":
		var info config.QiNiu
		// 绑定 json 参数
		modifyData := c.ShouldBindJSON(&info)
		if modifyData != nil {
			http.FailWithCode(http.ParamsError, c)
			return
		}
		common.Config.QiNiu = info
	case "jwt":
		var info config.Jwt
		// 绑定 json 参数
		modifyData := c.ShouldBindJSON(&info)
		if modifyData != nil {
			http.FailWithCode(http.ParamsError, c)
			return
		}
		common.Config.Jwt = info
	default:
		http.FailWithMessage("不存在对应的配置文件", c)
		return
	}

	err = core.ModifySiteInfo()
	if err != nil {
		common.Log.Error(err.Error())
		http.FailWithMessage(err.Error(), c)
		return
	}
	http.IsOkWithMessage("修改成功", c)
}
